import 'package:boss/models/taste.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class TasteProvider extends ChangeNotifier {
  Taste taste;

  TasteProvider({Taste taste}) {
    if (taste == null) {
      var temp = Taste.fromJson({
        'tasteName': '',
        'tasteLabel': '',
      });
      temp.labels = [Label()];
      this.taste = temp;
    } else {
      this.taste = Taste.fromJson(taste.toJson());
      this.taste.labels = this.taste.tasteLabel.split('|').map((item) => Label(label: item)).toList();
    }
  }

  setLabelIndex(int tasteIndex, int labelIndex, String value) {
    notifyListeners();
  }

  addLabel() {
    taste.labels.add(Label(label: ''));

    notifyListeners();
  }

  removeLabel(int labelIndex) {
    taste.labels.removeAt(labelIndex);
    notifyListeners();
  }

  bool check() {
    if (taste.tasteName == null || taste.tasteName == '' || taste.tasteName.length > 20) {
      toast('口味名称不能为空或长度不能大于20，请检查！');
      return false;
    }

    return taste.labels.every((label) {
      if (label.label.length == 0) {
        toast('口味标签不能为空');
        return false;
      }
      if (label.label.length > 10) {
        toast('口味标签长度不能大于10，请检查！');
        return false;
      }
      return true;
    });
  }

  Taste transferLabel() {
    taste.tasteLabel = taste.labels.map((label) => label.label).join('|');

    return taste;
  }

  clearAndAddLabels(List<String> lables) {
    taste.labels = lables.map((item) => Label(label: item)).toList();
    notifyListeners();
  }
}
